from handlers.mongo_handler import MongoDBHandler
from handlers.qdrant_handler import QdrantHandler
import datetime
from config import DB_CONFIG, Qdrant_CONFIG
import re

EMBEDDING_MODEL = "BAAI/bge-m3"
# EMBEDDING_MODEL = "/mnt/hf_models/huggingface/hub/models--BAAI--bge-m3"

# 初始化
mongo_handler = MongoDBHandler(DB_CONFIG['url'])
qdrant_handler = QdrantHandler(Qdrant_CONFIG['url'], Qdrant_CONFIG['collection_name'], EMBEDDING_MODEL)

def main():
    # original_docs = mongo_handler.get_documents(DB_CONFIG["db_name"], DB_CONFIG["collection_name"])
    # print(f"发现 {len(original_docs)} 条原始数据")

    # for idx, doc in enumerate(original_docs, 1):
    #     print(f"处理第 {idx}/{len(original_docs)} 条数据")
    #     extracted = mongo_handler.extract_content(doc['feed_url'])
    #     # extracted = re.sub(r'\n\s*\n+', '\n', re.sub(r'\n{2,}(#)', r'\n\1', extracted or '')).strip()
    #     print(f"提取内容: {extracted}")
    #     mongo_handler.save_documents(doc, extracted, DB_CONFIG["target_db"], DB_CONFIG["target_collection"])
    
    
    new_docs = mongo_handler.get_documents(DB_CONFIG["target_db"], DB_CONFIG["target_collection"])
    print(f"发现 {len(new_docs)} 条新数据")
    # print(f"new_docs 的内容: {new_docs}")
    # print(f"new_docs 的类型: {type(new_docs)}")
    qdrant_handler.insert_documents(new_docs)
    results = qdrant_handler.search("健身", top_k=5)
    for i, item in enumerate(results, 1):
        print(f"\n第 {i} 条结果：")
        print("feed_url:", item['feed_url'])
        print("内容摘要:\n", item['extracted_content'])


if __name__ == "__main__":
    main()
    
    
